<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'del_delivery':
      del_delivery($myPdo);
      break;
    case 'set_available':
      set_available($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function del_delivery($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
 
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $q);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_delivery', $cond_vals);
  
  echo json_encode(array());
}


function set_available($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
 
  $myPdo->exec('UPDATE ' . TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_delivery SET status = ' . $q->available . ' WHERE id = ' . $q->id);
  
  echo json_encode(array());
}
?>